package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.IBrandService;
import com.leyou.item.mapper.CategoryBrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.CategoryBrand;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    CategoryBrandMapper categoryBrandMapper;

    @Override
    public PageResult<Brand> queryBrandByPage(String key, Integer page, Integer rows, String soryBy, Boolean desc) {

        PageHelper.startPage(page, rows);

        Example example = new Example(Brand.class);

        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%").orEqualTo("letter", key.toUpperCase());
        }
        if (StringUtils.isNotBlank(soryBy)) {
            String orderByClause = soryBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }

        List<Brand> brandList = brandMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(brandList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        PageInfo<Brand> info = new PageInfo<>(brandList);

        return new PageResult<Brand>(info.getTotal(), info.getList());
    }

    @Override
    @Transactional
    public void addBrand(Brand brand, Long[] ids) {

        brand.setId(null);
        int count = brandMapper.insert(brand);

        if (count == 0) {
            throw new LyException(ExceptionEnum.INTERNAL_SERVER_ERROR);
        }

        List<CategoryBrand> categoryBrandList = Arrays.stream(ids)
                .map(categoryId -> new CategoryBrand(categoryId, brand.getId()))
                .collect(Collectors.toList());
        count = categoryBrandMapper.insertList(categoryBrandList);

        if (count != categoryBrandList.size()) {
            throw new LyException(ExceptionEnum.INTERNAL_SERVER_ERROR);
        }

        /*for (Long id : ids) {
            count = brandMapper.addCategoryBrand(id, brand.getId());
            if (count == 0){
                throw new LyException(ExceptionEnum.INTERNAL_SERVER_ERROR);
            }
        }*/
    }

    @Override
    public Brand queryBrandById(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据分类cid查询品牌
     *
     * @param cid
     * @return
     */
    @Override
    public List<Brand> queryBrandListByCid(Long cid) {

        CategoryBrand categoryBrand = new CategoryBrand();
        categoryBrand.setCategoryId(cid);
        List<CategoryBrand> categoryBrandList = categoryBrandMapper.select(categoryBrand);

        if (CollectionUtils.isEmpty(categoryBrandList)) {
            throw new LyException(ExceptionEnum.CATEGORY_BRAND_NOT_FOUND);
        }
        List<Long> ids = categoryBrandList.stream().map(CategoryBrand::getBrandId).collect(Collectors.toList());

        List<Brand> brands = brandMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.CATEGORY_BRAND_NOT_FOUND);
        }

        return brands;
    }

    @Override
    public List<Brand> queryBrandByIds(List<Long> ids) {

        List<Brand> brands = brandMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brands;
    }
}
